package com.baitengsoft.patentapimonitor.api.service.impl;

import com.baitengsoft.patentapimonitor.api.dao.LogUserinfoHistoryDao;
import com.baitengsoft.patentapimonitor.api.entity.LogUserinfoHistoryEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.SearchPage;
import com.baitengsoft.patentapimonitor.api.model.dto.WebDto;
import com.baitengsoft.patentapimonitor.api.model.dto.WebUserUpdateLoginInfo;
import com.baitengsoft.patentapimonitor.api.service.LogUserinfoHistoryService;
import com.baitengsoft.patentapimonitor.api.service.WebUserService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.WebUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Admin on 2017/12/28.
 */
@Service
public class LogUserinfoHistoryServiceImpl extends BaseServiceImpl<LogUserinfoHistoryEntity> implements LogUserinfoHistoryService {

    @Autowired
    org.dozer.Mapper mapper;
    @Autowired
    LogUserinfoHistoryDao logUserinfoHistoryDao;

    @Autowired
    WebUserService webUserService;


    @Override
    public PageResult<LogUserinfoHistoryEntity> searchHistoryLog(String userName, SearchPage param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        Mapper<LogUserinfoHistoryEntity> mapperTk = getMapper();
        Example example = new Example(LogUserinfoHistoryEntity.class);
        example.setOrderByClause("createdate DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name" , userName);
        List<LogUserinfoHistoryEntity> entities = mapperTk.selectByExample(example);
        PageInfo<LogUserinfoHistoryEntity> pageInfo = new PageInfo<>(entities);
        if (entities != null && entities.size() > 0)
            return new PageResult<LogUserinfoHistoryEntity>(entities, param.getPage(), param.getPageSize(), (int) pageInfo.getTotal());
        return null;
    }

    @Override
    public List<WebDto> searchById(String idStr) {

        if (StringUtils.isNotBlank(idStr)) {
            List<WebDto> list = new ArrayList<WebDto>();
            String[] ids = idStr.split(",");
            for (String id : ids) {
                WebDto webDto = new WebDto();
                LogUserinfoHistoryEntity entity = queryById(Integer.parseInt(id));
                mapper.map(entity, webDto);
                webDto.setCreatedatestr(getDate(entity.getCreatedate()));
                webDto.setLogindatestr(getDate(entity.getLogindate()));
                list.add(webDto);
            }
            return list;
        }
        return null;
    }

    @Transactional
    public boolean saveLog(HttpServletRequest request, WebUserinfoEntity userInfo) {
        int count = logUserinfoHistoryDao.getCount(userInfo.getName()) + 1;
        LogUserinfoHistoryEntity logEntity = new LogUserinfoHistoryEntity();
        logEntity.setName(userInfo.getName());
        logEntity.setNickname(userInfo.getNickname());
        //logEntity.setStatus(userInfo.getStatus());
        logEntity.setPlatformid(userInfo.getPlatformid());
        logEntity.setLogindate(new Date());
        logEntity.setIpadress(WebUtils.getIpAddr(request));
        logEntity.setBrower(WebUtils.getBrowser(request));
        logEntity.setLogincount(count);
        logEntity.setCreatedate(new Date());
        logEntity.setUpdatedate(new Date());
        logEntity.setCreateoperid(userInfo.getId());
        logEntity.setUpdateoperid(userInfo.getId());
        //同步更新 用户表-用户登录相关信息
        WebUserUpdateLoginInfo loginInfo = new WebUserUpdateLoginInfo(userInfo.getId(), logEntity.getIpadress(), logEntity.getBrower(), new Date(), count);
        webUserService.updateLoginInfo(loginInfo);
        return this.saveByNotNull(logEntity) > 0;


    }


    private static String getDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        return sdf.format(date);
    }


}
